package com.allwees.bs.module.ums.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.allwees.bs.core.model.coupon.constant.ECashLogType;
import com.allwees.bs.module.base.service.impl.BoServiceImpl;
import com.allwees.bs.module.ums.dao.CustomerCashDao;
import com.allwees.bs.module.ums.dao.CustomerCashLogDao;
import com.allwees.bs.module.ums.entity.Customer;
import com.allwees.bs.module.ums.entity.CustomerCash;
import com.allwees.bs.module.ums.entity.CustomerCashLog;
import com.allwees.bs.module.ums.req.CustomerCashCrudReq;
import com.allwees.bs.module.ums.req.query.CustomerCashLogQueryReq;
import com.allwees.bs.module.ums.service.CustomerCashService;
import com.allwees.bs.module.ums.service.CustomerService;
import com.allwees.core.common.util.MoneyUtil;
import com.allwees.core.dao.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Service
public class CustomerCashServiceImpl extends BoServiceImpl<CustomerCashDao, CustomerCash> implements CustomerCashService {

    @Autowired
    private CustomerCashDao customerCashDao;
    @Autowired
    private CustomerCashLogDao customerCashLogDao;
    @Autowired
    private CustomerService customerService;

    @Override
    public CustomerCash findByUserUuid(String userUuid) {
        return customerCashDao.findByUserUuid(userUuid);
    }

    @Override
    public void addAmt(CustomerCashCrudReq req) {
        CustomerCash customerCash = findByUserUuid(req.getUserUuid());
        if (customerCash == null) {
            customerCash = new CustomerCash(req.getUserUuid(), req.getIncreaseAmt());
            customerCashDao.insert(customerCash);
        } else {
            customerCash.setValue(MoneyUtil.add(customerCash.getValue(), req.getIncreaseAmt()));
            customerCashDao.updateByUuid(customerCash, customerCash.getUuid());
        }

        saveLog(req.getUserUuid(), req.getIncreaseAmt(), ECashLogType.SYSTEM_INCREASE, req.getRemark());
    }

    @Override
    public void refunded(String userUuid, BigDecimal amtCash) {
        if (MoneyUtil.isNullOrLtEqZero(amtCash)) {
            return;
        }
        CustomerCash cash = findByUserUuid(userUuid);
        cash.setValue(MoneyUtil.add(amtCash, cash.getValue()));
        baseMapper.updateByUuid(cash, cash.getUuid());
    }

    @Override
    public Page<CustomerCashLog> logPage(Page<CustomerCashLog> page, CustomerCashLogQueryReq req) {
        page = customerCashLogDao.selectPage(page, req.toQueryWrapper());
        if (page.getTotal() == 0) {
            return PageUtil.create(page);
        }
        page.getRecords().forEach(record -> {
            Customer customer = customerService.findByUuid(record.getUserUuid());
            if (customer != null) {
                record.setUserName(customer.getLastName() + customer.getFirstName());
            }
        });
        return page;
    }

    private void saveLog(String userUuid, BigDecimal amtCash, ECashLogType logType,String remark) {
        CustomerCashLog userCashLog = new CustomerCashLog();
        userCashLog.setUserUuid(userUuid);
        userCashLog.setValue(amtCash);
        userCashLog.setLogType(logType);
        userCashLog.setRemark(remark);
        customerCashLogDao.insert(userCashLog);
    }
}
